package com.book.dao;

import com.book.entity.Book;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface BookMapper {
    // 展示所有书
    @Select("select id,name,book_number bookNumber from books;")
    List<Book> listBooks();

    // 统计书数量
    @Select("select sum(book_number) from books;")
    Integer countBooks();

    // 列出数量>0的书
    @Select("select * from books where book_number>0;")
    List<Book> listExistBooks();

    // 根据书的id，-1该书数量
    @Update("update books set book_number = book_number-1 where books.id=#{id};")
    void minusBookNumber(Integer id);

    // 根据书id查询该书数量
    @Select("select book_number from books where books.id = #{id};")
    Integer getNumberById(Integer id);

    // 插入书目
    @Insert("insert into books(id, name, book_number) values (#{id}, #{name}, #{bookNumber});")
    void insertBook(Book book);

    // 根据id查询书目
    @Select("select * from books where id=#{id};")
    Book selectBookById(Integer id);

    // 调整书籍数量
    @Update("update books set book_number = #{number} where books.id=#{id};")
    void modifyBookNumber(@Param("id") Integer id, @Param("number") Integer number);

    // 删除书目
    @Delete("delete from books where id=#{id}")
    void deleteById(Integer id);
}